<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- <a-form-item label="行号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['rowNum']" placeholder="请输入行号"></a-input>
        </a-form-item> -->
        <a-form-item label="工序编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['processCode']"
            :trigger-change="true"
            org-fields="process_code,process_name,process_content,id"
            dest-fields="processCode,processName,processShortext,processId"
            code="process_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['processName']" placeholder="请输入工序名称"></a-input>
        </a-form-item>
        <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <j-date placeholder="请选择开始时间" v-decorator="['startTime']" :trigger-change="true" style="width: 100%"/> -->
           <j-date placeholder="请选择开始时间" v-decorator="['startTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="工作中心" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['workCenter']" placeholder="请输入工作中心"></a-input>
        </a-form-item>
        <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['factoryCode']"
            :trigger-change="true"
            org-fields="id,fac_code,fac_nameone"
            dest-fields="factoryId,factoryCode,factoryName"
            code="factory_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"></a-input>
        </a-form-item>
        <a-form-item label="控制码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlCode']" placeholder="请输入控制码"></a-input>
        </a-form-item>
        <a-form-item label="标文本" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['markText']" placeholder="请输入标文本"></a-input>
        </a-form-item>
        <a-form-item label="工序短文本" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['processShortext']" placeholder="请输入工序短文本"></a-input>
        </a-form-item>
        <a-form-item label="系统状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['systemState']" placeholder="请输入系统状态"></a-input>
        </a-form-item>
        <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择结束时间" v-decorator="['endTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
          <!-- <j-date placeholder="请选择结束时间" v-decorator="['endTime']" :trigger-change="true" style="width: 100%"/> -->
        </a-form-item>
        <a-form-item label="工序数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['processNum']" placeholder="请输入工序数量"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="工序ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['processId']" placeholder="请输入工序ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "MesProduceProcessModal",
    components: {
      JDate,
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/order/mesOrderProduce/addMesProduceProcess",
          edit: "/order/mesOrderProduce/editMesProduceProcess",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','sysOrgCode','processCode','processName','startTime','workCenter','factoryId','factoryCode','factoryName','controlCode','markText','processShortext','systemState','endTime','processNum','proorderId','rowNum','query2','query3','query4','query5','query6','processId'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['proorderId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','sysOrgCode','processCode','processName','startTime','workCenter','factoryId','factoryCode','factoryName','controlCode','markText','processShortext','systemState','endTime','processNum','proorderId','rowNum','query2','query3','query4','query5','query6','processId'))
      },


    }
  }
</script>
